<template>
  <div class="hold-transition">
    <div id="app" style="width: 85vw;">
      <div class="content-header">
        <h1>车辆运营统计<small>车辆运营信息统计</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>统计管理</el-breadcrumb-item>
          <el-breadcrumb-item>车辆运营统计</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="app-container">
        <div class="box">
          <!-- 搜索和操作按钮 -->
          <div class="filter-container">
            <el-input
                placeholder="请输入查询条件"
                v-model="pagination.queryString"
                style="width: 200px;"
                class="filter-item"
            ></el-input>
            <el-button @click="findPage()" class="dalfBut">查询</el-button>
          </div>

          <!-- 数据表格 -->
          <el-table
              v-loading="loading"
              size="small"
              :data="dataList"
              stripe
              highlight-current-row
          >
            <el-table-column type="index" align="center" label="序号"></el-table-column>
            <el-table-column prop="date" label="日期" align="center"></el-table-column>
            <el-table-column prop="operatingHours" label="运营时长" align="center"></el-table-column>
            <el-table-column prop="mileage" label="行驶里程" align="center"></el-table-column>
            <el-table-column prop="fuelConsumption" label="燃油消耗" align="center"></el-table-column>
          </el-table>

          <!-- 分页组件 -->
          <div class="pagination-container">
            <el-pagination
                class="pagiantion"
                @current-change="findPage"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                layout="total, prev, pager, next, jumper"
            ></el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import service from '@/util/http';

// 表单引用
const loading = ref(false);

// 数据列表
const dataList = ref([]);

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
  queryString: ''
});

// 分页查询
const findPage = async (page = 1) => {
  loading.value = true;
  try {
    pagination.currentPage = page;
    const params = {
      currentPage: pagination.currentPage,
      pageSize: pagination.pageSize,
      queryString: pagination.queryString
    };
    const response = await service.get('/admin/vehicle-operation/page', { params });
    if (response.data.flag) {
      pagination.total = response.data.data.total;
      dataList.value = response.data.data.records;
    } else {
      ElMessage.error(response.data.message || '查询失败');
    }
  } catch (error) {
    console.error('查询失败:', error);
    ElMessage.error('网络错误，请稍后重试');
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  findPage();
});
</script>

<style scoped>
@import '../css/style.css';
</style>